package com.imooc.systemwidget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

public class ShineTextView extends TextView {

    private LinearGradient mLinearGradient;
    private Matrix mGradientMatrix;
    private Paint mPaint;
    private int mViewWidth = 0;
    private int mTranslate = 0;
    private final String TAG = "ShineTextView";
    private String i = "0" ;

    public ShineTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        Log.d(TAG, "onSizeChanged: ");
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {
                mPaint = getPaint();
                mLinearGradient = new LinearGradient(  //gradient.梯度|LinearGradient是一个渐变渲染器！！！
                        0,
                        0,
                        mViewWidth,
                        0,
                        new int[]{
                                Color.BLUE, 0xffffffff,
                                Color.RED},
                        null,
                        Shader.TileMode.CLAMP);

//                 Create a shader that draws a linear gradient along a line.
//                 @param x0           The x-coordinate for the start of the gradient line
//                 @param y0           The y-coordinate for the start of the gradient line
//                 @param x1           The x-coordinate for the end of the gradient line
//                 @param y1           The y-coordinate for the end of the gradient line
//                 @param  colors      The colors to be distributed along the gradient line         用于设置渐变中的色彩
//                 @param  positions   May be null. The relative positions [0..1] of
//                 each corresponding color in the colors array. If this is null,
//                 the the colors are distributed evenly along the gradient line.
//                 @param  tile        The Shader tiling mode
//
//    public LinearGradient(float x0, float y0, float x1, float y1, int colors[], float positions[],
//                Shader.TileMode tile)

                mPaint.setShader(mLinearGradient);//设置渲染器
                mGradientMatrix = new Matrix();
//                Log.d(TAG, "onSizeChanged: ");
//                String i ="88888888888";
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.d(TAG, "onDraw:000000 "+i);
        if (mGradientMatrix != null) {
            mTranslate += mViewWidth / 5;
//            Log.d(TAG, "onDraw: ");
            if (mTranslate > 2 * mViewWidth) {
                mTranslate = -mViewWidth;
            }
            mGradientMatrix.setTranslate(mTranslate, 0);//平移矩阵
            mLinearGradient.setLocalMatrix(mGradientMatrix);//通过设置平移的矩阵来平移渐变效果
            postInvalidateDelayed(100);
//            Log.d(TAG, "onDraw2222: ");
        }
    }
}

/*
postInvalidateDelayed(100);线程刷新，0.1s一刷，每刷调用onDraw一次****反复不停地调用onDraw()
07-16 07:19:59.949 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:19:59.949 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.065 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.065 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.181 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.181 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.298 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.298 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.414 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.414 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.532 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.532 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.648 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.649 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.764 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.764 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.881 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.881 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:00.997 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:00.997 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
07-16 07:20:01.115 28857-28857/com.example.helloworld D/ShineTextView: onDraw:000000
07-16 07:20:01.115 28857-28857/com.example.helloworld D/ShineTextView: onDraw:
 */